package lp.xian.equipment.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import lp.xian.equipment.domain.Adjunct;
import lp.xian.equipment.domain.Finance;
import lp.xian.equipment.mapper.AdjunctMapper;
import lp.xian.equipment.mapper.FinanceMapper;
import lp.xian.equipment.service.IAdjunctService;
import lp.xian.equipment.vo.AdjunctVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 设备附件列表Service业务层处理
 *
 * @author ruoyi
 * @date 2023-06-30
 */
@Service
public class AdjunctServiceImpl implements IAdjunctService
{
    @Autowired
    private AdjunctMapper adjunctMapper;
    @Autowired
    private FinanceMapper financeMapper;

    /**
     * 查询设备附件列表
     *
     * @param adjunctId 设备附件列表主键
     * @return 设备附件列表
     */
    @Override
    public Adjunct selectAdjunctByAdjunctId(Integer adjunctId)
    {
        return adjunctMapper.selectAdjunctByAdjunctId(adjunctId);
    }

    /**
     * 查询设备附件列表列表
     *
     * @param adjunct 设备附件列表
     * @return 设备附件列表
     */
    @Override
    public List<Adjunct> selectAdjunctList(Adjunct adjunct)
    {
        return adjunctMapper.selectAdjunctList(adjunct);
    }

    /**
     * 新增设备附件列表
     *
     * @param adjunct 设备附件列表
     * @return 结果
     */
    @Override
    public int insertAdjunct(Adjunct adjunct)
    {
        adjunct.setCreateTime(DateUtils.getNowDate());
        return adjunctMapper.insertAdjunct(adjunct);
    }

    /**
     * 修改设备附件列表
     *
     * @param adjunct 设备附件列表
     * @return 结果
     */
    @Override
    public int updateAdjunct(Adjunct adjunct)
    {
        adjunct.setUpdateTime(DateUtils.getNowDate());
        return adjunctMapper.updateAdjunct(adjunct);
    }

    /**
     * 批量删除设备附件列表
     *
     * @param adjunctIds 需要删除的设备附件列表主键
     * @return 结果
     */
    @Override
    public int deleteAdjunctByAdjunctIds(Integer[] adjunctIds)
    {
        return adjunctMapper.deleteAdjunctByAdjunctIds(adjunctIds);
    }

    /**
     * 删除设备附件列表信息
     *
     * @param adjunctId 设备附件列表主键
     * @return 结果
     */
    @Override
    public int deleteAdjunctByAdjunctId(Integer adjunctId)
    {
        return adjunctMapper.deleteAdjunctByAdjunctId(adjunctId);
    }

    @Override
    public int auditAdjunct(AdjunctVo adjunctvo) {
        Adjunct adjunct=new Adjunct();
        adjunct.setAdjunctId(adjunctvo.getAdjunctId());
        adjunct.setAuditOpinion(adjunctvo.getAuditOpinion());
        adjunct.setAuditStatus(adjunctvo.getAuditStatus());
        adjunct.setAuditTime(new Date());
        adjunct.setAuditPerson(SecurityUtils.getUsername());
        return adjunctMapper.updateAdjunct(adjunct);

    }

    @Override
    public int approverAdjunct(AdjunctVo adjunctvo) {
        //审批后数据需要提交到财务 收入支出
        Adjunct adjunct=new Adjunct();
        adjunct.setAdjunctId(adjunctvo.getAdjunctId());
        adjunct.setApproverOpinion(adjunctvo.getApproverOpinion());
        adjunct.setAuditStatus(adjunctvo.getAuditStatus());
        adjunct.setApproverTime(new Date());
        adjunct.setApproverPerson(SecurityUtils.getUsername());
        int num=adjunctMapper.updateAdjunct(adjunct);
        Adjunct adjunct1=adjunctMapper.selectAdjunctByAdjunctId(adjunctvo.getAdjunctId());
        Finance money= financeMapper.getFinanceMoney();
        if (adjunct1.getAuditStatus().intValue()==2) {
                Finance finance=new Finance();
                finance.setFinanceName("【购买】"+adjunct1.getAdjunctName());
                finance.setFinanceMoney(adjunct1.getAdjunctPrice());
                finance.setFinanceBalance(adjunct1.getAdjunctPrice());
                finance.setSupplierId(adjunct1.getSupplierId());
                finance.setFinanceStatus(0);
                finance.setAuditStatus(0);
                finance.setCreateTime(new Date());
                finance.setCreateBy(SecurityUtils.getUsername());
            finance.setFinanceBalance(money.getFinanceBalance());
            return financeMapper.insertFinance(finance);
        }

       return 1;
    }

}
